<?php

class AddTabForm extends CFormModel
{
	public $tab;
	public $tab2;
	
	public function rules()
	{
		return array(
			array('type, name, place_id', 'required'),
		);
	}
	
	public function add()
	{
		$tab = new Tab();
		$tab->attributes = $this->attributes;
		switch($this->type) {
			case 'normal': 
				$tab2 = new TabNormal();
				break;
			default:
				return false;
				break;
		}
		$transaction = Yii::app()->db->beginTransaction();
		try {
			if(!$tab->save()){
				throw new Exception('tab not save.');
			}
			$this->tab = $tab;
			$tab2->id = $tab->id;
			if(!$tab2->save()){
				throw new Exception('tab2 not save.');
			}
			$this->tab2 = $tab2;
			return true;
		}
		catch(Exception $e) {
			$transaction->rollback();
		}
		return false;
	}
	
	public function actionGetTab()
	{
		switch($this->type) {
			case 'normal': 
				$tab = TabNormal::model()->with('tab')->findByPk();
				break;
			default:
				$tab = null;
				break;
		}
		return $tab;
	}
}